//
//  FinderViewController.m
//  jiyujiaProgram
//
//  Created by 中良 赵 on 15/10/13.
//  Copyright (c) 2015年 中良 赵. All rights reserved.
//

#import "FinderViewController.h"
#import "FinderTableViewCell.h"
#import "SVProgressHUD.h"
#import "OrderModel.h"

@interface FinderViewController ()<UIAlertViewDelegate>
{
    MBProgressHUD *_HUD;
    
    MemberModel *_memberModel;
    TeacherModel *_teacherModel;
    
    OrderModel *_orderModel;
    NSString *_lat;
    NSString *_lng;
    NSMutableArray *_dataArray;
    NSInteger _index;

}
@end

@implementation FinderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self loadUserInfo];

    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
}
-(void)viewDidAppear:(BOOL)animated{
    [_tableview.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
    _teacherModel = userInfo.teacherModel;
    _lat = userInfo.lat;
    _lng = userInfo.lng;
}
- (void)creatFirstHttpRequest
{
    NSDictionary *param = @{
                            @"course_category":@"2",
                            @"promulgator_mark":@"3",
                            @"status":@"1",
//                            @"lng":_lng,
//                            @"lat":_lat,
                            @"pageIndex":[NSString stringWithFormat:@"%ld",_index],
                            @"pageSize":@"10",
                            };
    [[HttpManager defaultManager]postRequestToUrl:RequestURL(COURSE) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [OrderModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"OrderData",
                         };
            }];
            
            if (_dataArray) {
                [_dataArray removeAllObjects];
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            _orderModel = [[OrderModel alloc] init];
            _orderModel = [OrderModel objectWithKeyValues:result];
            [_dataArray addObjectsFromArray:_orderModel.data];
            if (_dataArray.count==0) {
                [SVProgressHUD showSuccessWithStatus:@"暂无可发现的课程"];
            }
            [_tableview.mj_header endRefreshing];
            [_tableview.mj_footer endRefreshing];
            [_tableview reloadData];
        }
    }];
}
- (void)headerRefreshing
{
    _index = 0;
    [self creatFirstHttpRequest];
}

- (void)footerRefreshing
{
    if (_dataArray.count%10 == 0) {
        _index ++;
        [self creatFirstHttpRequest];
    }else{
        [_tableview.mj_footer endRefreshing];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 176.f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    
    FinderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"FinderTableViewCell" owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    OrderData *data=[_dataArray objectAtIndex:indexPath.row];
    NSLog(@"%@",data);
    cell.lblClassDatetime.text=data.class_time;
    cell.btnBaoMing.tag=indexPath.row;
    [cell.btnBaoMing addTarget:self action:@selector(baoming:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(void)baoming:(id)sender{
    UIButton*btn=(UIButton *)sender;
    NSLog(@"%ld",(long)btn.tag);
    UIAlertView *av=[[UIAlertView alloc]initWithTitle:@"报名" message:@"确定报N3-N2的课程吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [av show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%lD",(long)buttonIndex);
    if (buttonIndex==1) {
        [SVProgressHUD showSuccessWithStatus:@"报名成功"];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
